package cn.news.service;

import cn.news.dao.CategoryMapper;
import cn.news.pojo.Category;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CategoryServiceImpl implements CategoryService{

    @Resource
    private  CategoryMapper cm;

    @Override
    public List getType() {

        Jedis jedis=new Jedis("127.0.0.1");
        List list=new ArrayList();
        Map<String,String> all=jedis.hgetAll("cate");

        if(all.size()==0){
            List<Category> clist=cm.getType();
            for (Category c:clist){
                    jedis.hset("cate",c.getId().toString(),JSON.toJSONString(c));
            }
        }
        all=jedis.hgetAll("cate");
        Iterator<Map.Entry<String, String>> entries = all.entrySet().iterator();
        while(entries.hasNext()){

            Map.Entry<String,String> entry =entries.next();
            String key=entry.getKey();
            String value=entry.getValue();
            Category c= JSON.parseObject(value,Category.class);
            list.add(c);
        }
        jedis.close();
        return list;
    }
}
